<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/6/8
 * Time: 14:25
 */

namespace app\api\controller;


use think\Db;

class Drill extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 创建训练
     * @return \think\response\Json
     * join_user   参加比赛的人
     * drill_cate    训练类型   1 帆船   2 赛艇
     * drill_city    定位的城市
     */
    public function createDrill(){
        $join_user = trim(input('join_user'), ',');
        $drill_cate = input('drill_cate');
        $drill_city = input('city');
        $create = [
            'drill_create_user_id' => $this->user_id,
//            'drill_join_alluser_id' => ',' . $this->user_id . ',' . $join_user . ',',
//            'drill_join_user_id' => ',' . $join_user . ',',
            'drill_city' => $drill_city,
            'drill_create_time' => $this->create_time,
            'drill_cate' => $drill_cate,
            'drill_write_log_user' => ',0,',
            'drill_create_date' => date('Y-m-d')
        ];
        if($join_user != ''){
            $create['drill_join_alluser_id'] = ',' . $this->user_id . ',' . $join_user . ',';
            $create['drill_join_user_id'] = ',' . $join_user . ',';
        }else{
            $create['drill_join_alluser_id'] = ',' . $this->user_id . ',';
        }
        $drill_id = Db::table('up_sailboat_drill')->insertGetId($create);
        if($drill_id){
            return json(['status' => 200, 'msg' => $drill_id]);
        }else{
            return json(['status' => 0, 'msg' => '创建失败']);
        }
    }
    
    /**
     * 参加训练的队员
     * @param $drill_id     训练ID
     * @return \think\response\Json
     */
    public function showDrill($drill_id){
        $drill_user = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_join_alluser_id, drill_status, drill_join_user_id')
            ->find();
        $user_ids = trim($drill_user['drill_join_alluser_id'], ',');
        $join_ids = trim($drill_user['drill_join_user_id'], ',');
        // 包含我
        $users = Db::table('up_u_user')
            ->where('user_id', 'IN', $user_ids)
            ->field('user_id, user_image, user_username')
            ->select();
        // 不包含我
        $no_my = Db::table('up_u_user')
            ->where('user_id', 'IN', $join_ids)
            ->field('user_id, user_image, user_username')
            ->select();
        $user_arr = explode(',', $user_ids);
        
        // 我是属于哪个分队
        $shu = Db::table('up_sailboat_drill_group')
            ->where('sg_drill_id', $drill_id)
            ->where('sg_drill_user_ids', 'like', '%,' . $this->user_id . ',%')
            ->field('sg_id')
            ->find();
        if($shu){
            $sg_id = $shu['sg_id'];
        }else{
            $sg_id = '';
        }
        $return = [
            'status' => 200,
            'msg' => $users,
            'user_ids' => $user_ids,
            'drill_status' => $drill_user['drill_status'],
            'user_arr' => $user_arr,
            'sg_id' => $sg_id,
            'no_my' => $no_my
        ];
        return json($return);
    }
    
    /**
     * 开始/结束比赛
     * @param $drill_id    训练ID
     * @param $drill_status    训练状态   2 开始比赛   3 结束比赛
     * @return \think\response\Json
     */
    public function beginOrEndDrill($drill_id, $drill_status){
        $update = ['drill_status' => $drill_status];
        if($drill_status == 2){
            $update['drill_begin_time'] = $this->create_time;
//            $have_fen = Db::table('up_sailboat_drill_group')
//                ->where('sg_drill_id', $drill_id)
//                ->field('sg_drill_user_ids')
//                ->select();
//            if(!$have_fen){
//                return json(['status' => 0, 'msg' => '请先创建分队']);
//            }else{
//                $have_user = 1; // 假设有队员
//                foreach($have_fen as $k => $v){
//                    $user_ids = trim($v['sg_drill_user_ids'], ',');
//                    if(!$user_ids){
//                        $have_user = 2; // 没有队员
//                        break;
//                    }
//                }
//                if($have_user == 2){
//                    return json(['status' => 0, 'msg' => '请选择分队的训练队员']);
//                }
//            }
            $res = Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->update($update);
            if($res){
                return json(['status' => 200, 'msg' => true]);
            }else{
                return json(['status' => 0, 'msg' => false]);
            }
        }else{
            // 是否有未结束的训练赛轮次，如果有，需要先结束轮次
            $is_have = Db::table('up_sailboat_drill_turns')
                ->where([
                    'dt_drill_id' => $drill_id,
                    'dt_turns' => ['<>', 0],
                    'dt_status' => ['IN', '1,2']
                ])
                ->find();
            if($is_have) return json(['status' => 0, 'msg' => '请先结束所有训练赛']);
            
            Db::startTrans();
            try{
                // 结束训练
                $update['drill_end_time'] = $this->create_time;
                Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->update($update);
                
                // 结束训练赛，并且查找轮次为0的记录，删除该轮的所有其他记录
                // 修改训练赛状态
                Db::table('up_sailboat_drill_match')
                    ->where('dm_drill_id', $drill_id)
                    ->update(['dm_status' => 3, 'dm_end_time' => microtime(true)]);
                // 删除所有未开始的轮次记录
                $turns = Db::table('up_sailboat_drill_turns')
                    ->where(['dt_drill_id' => $drill_id, 'dt_turns' => 0])
                    ->field('dt_id')
                    ->select();
                if($turns){
                    foreach($turns as $kk => $vv){
                        // 删除未开始的轮次
                        Db::table('up_sailboat_drill_turns')->delete($vv['dt_id']);
                        // 删除浮标和小船信息
                        Db::table('up_sailboat_drill_match_sensor_device')->where('dms_dt_id', $vv['dt_id'])->delete();
                        // 删除用户信息
                        Db::table('up_sailboat_drill_match_user_detail')->where('dmd_dt_id', $vv['dt_id'])->delete();
                    }
                }
                Db::commit();
                return json(['status' => 200, 'msg' => true]);
            }catch(\Exception $e){
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        }
    }
    
    /**
     * 判断某个训练是否已经结束
     * @param $drill_id    训练ID
     * @return \think\response\Json
     */
    public function isOver($drill_id){
        $drill = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_status, drill_create_user_id')
            ->find();
        if($drill['drill_create_user_id'] != $this->user_id){
            if($drill['drill_status'] == 1){
                return json(['status' => 0, 'msg' => '训练未开始，请稍后进入']);
            }
        }
        if($drill['drill_status'] == 3){
            return json(['status' => 0, 'msg' => '该训练已结束']);
        }
        if($drill['drill_status'] == 4){
            return json(['status' => 0, 'msg' => '该训练已失效']);
        }
        if($drill['drill_status'] == 5){
            return json(['status' => 0, 'msg' => '该训练已被删除']);
        }
        return json(['status' => 200, 'msg' => true]);
    }
    
    /**
     * 设置赛艇训练心率、速度、桨频
     * @return \think\response\Json
     * drill_set_xinlv    心率
     * drill_set_speed    速度
     * drill_set_fre      桨频
     * drill_id           训练ID
     */
    public function setSTinfo(){
        $drill_set_xinlv = input('drill_set_xinlv');
        $drill_set_speed = input('drill_set_speed');
        $drill_set_fre = input('drill_set_fre');
        $drill_id = input('drill_id');
        $update = [];
        if(isset($drill_set_xinlv) && $drill_set_xinlv != ''){
            $update['drill_set_xinlv'] = $drill_set_xinlv;
        }
        if(isset($drill_set_speed) && $drill_set_speed != ''){
            $update['drill_set_speed'] = $drill_set_speed;
        }
        if(isset($drill_set_fre) && $drill_set_fre != ''){
            $update['drill_set_fre'] = $drill_set_fre;
        }
        $res = Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '设置成功']);
        }else{
            return json(['status' => 0, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 创建分队
     * @return \think\response\Json
     * drill_id    训练ID
     * group_user    被选中的用户
     * boat_type    赛艇的船型
     */
    public function createDrillGroup(){
        $drill_id = input('drill_id');
        $group_user = trim(input('group_user'), ',');
        $boat_type = input('boat_type');
        $drill_cate = Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->value('drill_cate');
        $create = [
            'sg_drill_id' => $drill_id,
            'sg_drill_cate' => $drill_cate
        ];
        if($group_user){
            $create['sg_drill_user_ids'] = ',' . $group_user . ',';
        }
        if($drill_cate == 2){
            $create['sg_boat_type'] = $boat_type;
        }
        
        Db::startTrans();
        try{
            // 分组
            $sg_id = Db::table('up_sailboat_drill_group')->insertGetId($create);
            // 赛艇还需入分组明细
            if($drill_cate == 2){
//                $info = [];
//                $user_arr = explode(',', $group_user);
//                foreach($user_arr as $k => $v){
//                    $info[$k] = [
//                        'ds_drill_id' => $drill_id,
//                        'ds_group_id' => $sg_id,
//                        'ds_user_id' => $v
//                    ];
//                }
//                Db::table('up_sailboat_drill_group_info')->insertAll($info);
            }
            Db::commit();
            return json(['status' => 200, 'msg' => $sg_id]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '创建失败']);
        }
    }
    
    /**
     * 已经被选入到分队的所有用户
     * @return \think\response\Json
     * drill_id     训练ID
     */
    public function drillGroupUser(){
        $drill_id = input('drill_id');
        $drill_user_ids = Db::table('up_sailboat_drill_group')
            ->where('sg_drill_id', $drill_id)
            ->column('sg_drill_user_ids');
        $ids = '';
        if($drill_user_ids){
            foreach($drill_user_ids as $k => $v){
                $v = trim($v, ',');
                $ids .=  $v . ',';
            }
            $ids = trim($ids, ',');
        }
        return json(['status' => 200, 'msg' => $ids]);
    }
    
    /**
     * 该次训练的所有队员---此接口已合并到showDrill中
     * @return \think\response\Json
     * drill_id   训练ID
     */
    public function joinUser(){
        $drill_id = input('drill_id');
        $join_user = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->value('drill_join_user_id');
        $user = trim($join_user, ',');
        return json(['status' => 200, 'msg' => explode(',', $user)]);
    }
    
    /**
     * 添加总的训练人员
     * @return \think\response\Json
     * drill_id     训练ID
     * add_user     新添加的人
     */
    public function addDrillUser(){
        $drill_id = input('drill_id');
        $add_user = trim(input('add_user'), ',');
        $drill_user = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_join_alluser_id, drill_join_user_id')
            ->find();
        
        if($add_user){
            $update['drill_join_alluser_id'] = $drill_user['drill_join_alluser_id'] . $add_user . ',';
            if($drill_user['drill_join_user_id']){
                $update['drill_join_user_id'] = $drill_user['drill_join_user_id'] . $add_user . ',';
            }else{
                $update['drill_join_user_id'] = ',' . $add_user . ',';
            }
            $res = Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->update($update);
            if($res){
                return json(['status' => 200, 'msg' => '添加成功']);
            }else{
                return json(['status' => 0, 'msg' => '添加失败']);
            }
        }else{
            return json(['status' => 200, 'msg' => '添加成功']);
        }
    }
    
    /**
     * 移除总的训练人员
     * @return \think\response\Json
     * drill_id    训练ID
     * del_user    被移除的用户
     */
    public function delDrillUser(){
        $drill_id = input('drill_id');
        $del_user = trim(input('del_user'), ',');
        $drill_user = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_join_alluser_id, drill_join_user_id')
            ->find();
        $del_arr = explode(',', $del_user);
        $alluser_arr = explode(',', trim($drill_user['drill_join_user_id'], ','));
        $same = array_intersect($del_arr, $alluser_arr);
        $new_user = array_diff($alluser_arr, $same);

        if($new_user){
            $update['drill_join_alluser_id'] = ',' . $this->user_id . ',' . implode(',', $new_user) . ',';
            $update['drill_join_user_id'] = ',' . implode(',', $new_user) . ',';
        }else{
            $update['drill_join_user_id'] = null;
            $update['drill_join_alluser_id'] = ',' . $this->user_id . ',';
        }

        Db::startTrans();
        try{
            // 更新总队
            Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->update($update);
            // 更新分队
            foreach($del_arr as $k => $v){
                $fen = Db::table('up_sailboat_drill_group')
                    ->where('sg_drill_user_ids', 'like', '%,' . $v . ',%')
                    ->where('sg_drill_id', $drill_id)
                    ->field('sg_drill_user_ids, sg_id')
                    ->find();
                $user_arr = explode(',', trim($fen['sg_drill_user_ids'], ','));
                $kk = array_search($v, $user_arr);
                unset($user_arr[$kk]);
                
                if($user_arr){
                    $user_ids = ',' . implode(',', $user_arr) . ',';
                }else{
                    $user_ids = null;
                }
                Db::table('up_sailboat_drill_group')
                    ->where('sg_id', $fen['sg_id'])
                    ->update([
                        'sg_drill_user_ids' => $user_ids
                    ]);
            }
            Db::commit();
            return json(['status' => 200, 'msg' => '移除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '移除失败']);
        }
    }
    
    /**
     * 删除分队
     * @return \think\response\Json
     * sg_id    分队ID
     */
    public function delGroup(){
        $sg_id = input('sg_id');
        $drill_cate = Db::table('up_sailboat_drill_group')->where('sg_id', $sg_id)->value('sg_drill_cate');
        Db::startTrans();
        try{
            // 删除分队
            Db::table('up_sailboat_drill_group')->delete($sg_id);
            // 删除分队中的成员
            if($drill_cate == 2){
                Db::table('up_sailboat_drill_group_info')->where('ds_group_id', $sg_id)->delete();
            }
            Db::commit();
            return json(['status' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 某一个分队中的人员
     * @return \think\response\Json
     * sg_id    分队ID
     */
    public function groupUser(){
        $sg_id = input('sg_id');
        $user = Db::table('up_sailboat_drill_group')
            ->field('sg_drill_user_ids')
            ->where('sg_id', $sg_id)
            ->find();
        $group_user = trim($user['sg_drill_user_ids'], ',');
        $info = Db::table('up_u_user')
            ->where('user_id', 'IN', $group_user)
            ->field('user_id, user_image, user_username')
            ->select();
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 所有未被选过的人----分队
     * @return \think\response\Json
     * drill_id     训练ID
     */
    public function unChoose(){
        $drill_id = input('drill_id');
        // 所有参加训练的人
        $all = $drill_user = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_join_alluser_id')
            ->find();
        $all_ids = trim($all['drill_join_alluser_id'], ',');
        $all_arr = explode(',', $all_ids);
        // 所有被选过的分队队员
        $fen = Db::table('up_sailboat_drill_group')
            ->where('sg_drill_id', $drill_id)
            ->column('sg_drill_user_ids');
        if($fen){
            $ids = '';
            foreach($fen as $k => $v){
                $v = trim($v, ',');
                $ids .=  $v . ',';
            }
            $ids = trim($ids, ',');
            $fen_arr = explode(',', $ids);
            $return = array_diff($all_arr, $fen_arr);
        }else{
            $return = $all_arr;
        }
        $user = Db::table('up_u_user')
            ->where('user_id', 'IN', $return)
            ->field('user_id, user_image, user_username')
            ->select();
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 好友中没有被选入总队的人员
     * @return \think\response\Json
     * keyword     搜索关键字
     * drill_id    训练ID
     */
    public function unchooseFirend(){
        $keyword = input('keyword');
        $drill_id = input('drill_id');
        // 我的好友
        $where['f_my_id'] = $this->user_id;
        $where['f_status'] = 2;
        $firend_id = Db::table('up_friend')
            ->where($where)
            ->column('f_friend_id');
        // 总队被选中的人
        $zong = $drill_user = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_join_alluser_id')
            ->find();
        $zong_ids = trim($zong['drill_join_alluser_id'], ',');
        $zong_arr = explode(',', $zong_ids);
        
        if($zong_arr){
            $return = array_diff($firend_id, $zong_arr);
        }else{
            $return = $firend_id;
        }
        
        $where['f_friend_id'] = ['IN', $return];
        if(isset($keyword) && $keyword != ''){
            $where['f_remark_name|user_username'] = ['like', '%' . $keyword . '%'];
        }
        $friendList = Db::table('up_friend')
            ->join('up_u_user', 'user_id = f_friend_id', 'left')
            ->where($where)
            ->field('f_id, user_image, user_username, f_remark_name, f_friend_id')
            ->select();
        if($friendList){
            return json(['status' => 200, 'msg' => $friendList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 训练下的所有分队
     * @return \think\response\Json
     * drill_id    训练ID
     */
    public function drillGroup(){
        $drill_id = input('drill_id');
        $group = Db::table('up_sailboat_drill_group')
            ->where('sg_drill_id', $drill_id)
            ->field('sg_id, sg_drill_user_ids, sg_drill_cate, sg_use_time, sg_ave_speed, sg_stroke_rate')
            ->select();
        if($group){
            foreach($group as $k => $v){
                $sg_drill_user_ids = trim($v['sg_drill_user_ids'], ',');
                if($v['sg_drill_cate'] == 1){
                    $user = Db::table('up_u_user')
                        ->where('user_id', 'IN', $sg_drill_user_ids)
                        ->field('user_id, user_image, user_username')
                        ->select();
                }else{
                    $user = Db::table('up_sailboat_drill_group_info')
                        ->join('up_u_user', 'user_id = ds_user_id', 'left')
                        ->where('ds_user_id', 'IN', $sg_drill_user_ids)
                        ->field('user_id, user_image, ds_valid_time, ds_rate, ds_valid_distance, user_username')
                        ->select();
                }
                $group[$k]['group_user'] = $user;
                $group[$k]['group_name'] = '分队' . ($k + 1);
            }
            return json(['status' => 200, 'msg' => $group]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 添加分队中的队员
     * @return \think\response\Json
     * sg_id     分队ID
     * add_user   新添加的人
     */
    public function addGroupUser(){
        $sg_id = input('sg_id');
        $add_user = trim(input('add_user'), ',');
        $group = Db::table('up_sailboat_drill_group')
            ->field('sg_drill_cate, sg_drill_user_ids, sg_drill_id')
            ->where('sg_id', $sg_id)
            ->find();
        
        if($add_user){
            if($group['sg_drill_user_ids']){
                $update = ['sg_drill_user_ids' => $group['sg_drill_user_ids'] . $add_user . ','];
            }else{
                $update = ['sg_drill_user_ids' => ',' . $add_user . ','];
            }
            Db::startTrans();
            try{
                // 更新
                Db::table('up_sailboat_drill_group')->where('sg_id', $sg_id)->update($update);
                // 如果是赛艇
                if($group['sg_drill_cate'] == 2){
                    $info = [];
                    $user_arr = explode(',', $add_user);
                    foreach($user_arr as $k => $v){
                        $info[$k] = [
                            'ds_drill_id' => $group['sg_drill_id'],
                            'ds_group_id' => $sg_id,
                            'ds_user_id' => $v
                        ];
                    }
                    Db::table('up_sailboat_drill_group_info')->insertAll($info);
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '添加成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '添加失败']);
            }
        }else{
            return json(['status' => 200, 'msg' => '添加成功']);
        }
    }
    
    /**
     * 删除分队中的人
     * @return \think\response\Json
     * sg_id      分队ID
     * del_user   被移除的人
     */
    public function delGroupUser(){
        $sg_id = input('sg_id');
        $del_user = trim(input('del_user'), ',');
        $group = Db::table('up_sailboat_drill_group')
            ->field('sg_drill_cate, sg_drill_user_ids')
            ->where('sg_id', $sg_id)
            ->find();
        $del_arr = explode(',', $del_user);
        $alluser_arr = explode(',', trim($group['sg_drill_user_ids'], ','));
        $same = array_intersect($del_arr, $alluser_arr);
        $new_user = array_diff($alluser_arr, $same);
        if($new_user){
            $update = ['sg_drill_user_ids' => ',' . implode(',', $new_user) . ','];
        }else{
            $update = ['sg_drill_user_ids' => null];
        }
        
        Db::startTrans();
        try{
            Db::table('up_sailboat_drill_group')->where('sg_id', $sg_id)->update($update);
            if($group['sg_drill_cate'] == 2){
                Db::table('up_sailboat_drill_group_info')
                    ->where('ds_group_id', $sg_id)
                    ->where('ds_user_id', 'IN', $del_user)
                    ->delete();
            }
            Db::commit();
            return json(['status' => 200, 'msg' => '移除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '移除失败']);
        }
    }
    
    /**
     * 保存训练计划和总结
     * @return \think\response\Json
     * drill_id       训练ID
     * drill_date     训练日期
     * drill_time     训练时间
     * drill_addr     训练地点
     * drill_lon      经度
     * drill_lat      纬度
     * drill_other_coach    其他教练员
     * drill_temperature    气温
     * drill_wind      风速
     * drill_flow_direction    流向
     * drill_flow_speed    流速
     * drill_yongl      涌浪
     * drill_chaoxi     潮汐
     * drill_sailing    航行里程
     * drill_plan       训练计划
     * drill_zongjie    训练总结
     */
    public function drillPlan(){
        $params = input('');
        $update = [];
        // 训练日期
        if(isset($params['drill_date'])){
            $update['drill_date'] = $params['drill_date'];
        }
        // 训练时间
        if(isset($params['drill_time'])){
            $update['drill_time'] = $params['drill_time'];
        }
        // 训练地点
        if(isset($params['drill_addr'])){
            $update['drill_addr'] = $params['drill_addr'];
        }
        // 经度
        if(isset($params['drill_lon'])){
            $update['drill_lon'] = $params['drill_lon'];
        }
        // 纬度
        if(isset($params['drill_lat'])){
            $update['drill_lat'] = $params['drill_lat'];
        }
        // 其他教练员
        if(isset($params['drill_other_coach'])){
            $update['drill_other_coach'] = $params['drill_other_coach'];
        }
        // 气温
        if(isset($params['drill_temperature'])){
            $update['drill_temperature'] = $params['drill_temperature'];
        }
        // 风速
        if(isset($params['drill_wind'])){
            $update['drill_wind'] = $params['drill_wind'];
        }
        // 流向
        if(isset($params['drill_flow_direction'])){
            $update['drill_flow_direction'] = $params['drill_flow_direction'];
        }
        // 流速
        if(isset($params['drill_flow_speed'])){
            $update['drill_flow_speed'] = $params['drill_flow_speed'];
        }
        // 涌浪
        if(isset($params['drill_yongl'])){
            $update['drill_yongl'] = $params['drill_yongl'];
        }
        // 潮汐
        if(isset($params['drill_chaoxi'])){
            $update['drill_chaoxi'] = $params['drill_chaoxi'];
        }
        // 航行里程
        if(isset($params['drill_sailing'])){
            $update['drill_sailing'] = $params['drill_sailing'];
        }
        // 训练计划
        if(isset($params['drill_plan'])){
            $update['drill_plan'] = $params['drill_plan'];
        }
        // 训练总结
        if(isset($params['drill_zongjie'])){
            $update['drill_zongjie'] = $params['drill_zongjie'];
        }
        $is_write_plan = Db::table('up_sailboat_drill')
            ->where('drill_id', $params['drill_id'])
            ->value('drill_is_write_plan');
        if($is_write_plan == 1){
            $update['drill_is_write_plan'] = 2;
        }
        $res = Db::table('up_sailboat_drill')->where('drill_id', $params['drill_id'])->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '保存成功']);
        }else{
            return json(['status' => 0, 'msg' => '保存失败']);
        }
    }
    
    /**
     * 训练列表
     * @return \think\response\Json
     */
    public function drillList(){
        $list = Db::table('up_sailboat_drill')
            ->where('drill_status', 'NEQ', 5)
            ->where('drill_join_alluser_id', 'like', '%,' . $this->user_id . ',%')
            ->field('drill_id, drill_create_user_id, drill_join_alluser_id, drill_create_time, drill_status, 
            drill_cate, drill_is_write_plan, drill_write_log_user, drill_date, drill_addr')
            ->order('drill_create_time desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            foreach($list as $k => $v){
                $join_user_id = trim($v['drill_join_alluser_id'], ',');
                $list[$k]['join_user_images'] = Db::table('up_u_user')
                    ->where('user_id', 'IN', $join_user_id)
                    ->column('user_image');
                // 是否发布过日志
                $write_user = explode(',', trim($v['drill_write_log_user'], ','));
                if(in_array($this->user_id, $write_user)){
                    $list[$k]['is_write'] = 1; // 写过
                }else{
                    $list[$k]['is_write'] = 2; // 没写过
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 展示训练计划
     * @return \think\response\Json
     * drill_id     训练ID
     */
    public function showPlan(){
        $drill_id = input('drill_id');
        $plan = Db::table('up_sailboat_drill')
            ->join('up_u_user', 'user_id = drill_create_user_id', 'left')
            ->where('drill_id', $drill_id)
            ->field('drill_date, drill_time, drill_addr, drill_lon, drill_lat, user_username, drill_other_coach, 
            drill_temperature, drill_wind, drill_flow_direction, drill_flow_speed, drill_yongl, drill_chaoxi, 
            drill_sailing, drill_plan, drill_zongjie')
            ->find();
        return json(['status' => 200, 'msg' => $plan]);
    }
    
    /**
     * 训练详情
     * @return \think\response\Json
     * drill_id
     */
    public function drillDetail(){
        $drill_id = input('drill_id');
        $drill = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->field('drill_id, drill_create_user_id, drill_join_user_id, drill_status, drill_cate')
            ->find();
        $user_ids = trim($drill['drill_join_user_id'], ',');
        $drill['user_image'] = Db::table('up_u_user')
            ->where('user_id', 'IN', $user_ids)
            ->field('user_id, user_image')
            ->select();
        // 分队
        $group = Db::table('up_sailboat_drill_group')
            ->field('sg_id, sg_drill_user_ids, sg_drill_cate, sg_use_time, sg_ave_speed, sg_stroke_rate')
            ->where('sg_drill_id', $drill['drill_id'])
            ->select();
        if($group){
            foreach($group as $k => $v){
                $sg_drill_user_ids = trim($v['sg_drill_user_ids'], ',');
                if($v['sg_drill_cate'] == 1){
                    $group[$k]['user_images'] = Db::table('up_u_user')
                        ->where('user_id', 'IN', $sg_drill_user_ids)
                        ->field('user_id, user_image')
                        ->select();
                }else{
                    $group[$k]['user_images'] = Db::table('up_sailboat_drill_group_info')
                        ->join('up_u_user', 'user_id = ds_user_id', 'left')
                        ->where('ds_user_id', 'IN', $sg_drill_user_ids)
                        ->field('user_id, user_image, ds_valid_time, ds_rate, ds_valid_distance')
                        ->select();
                }
            }
            $drill['group'] = $group;
        }else{
            $drill['group'] = [];
        }
        return json(['status' => 200, 'msg' => $drill]);
    }
    
    /**
     * 填写航行日志
     * @return \think\response\Json
     * drill_id       训练ID
     * sl_type        1  仅保存    2 保存并抄送
     * sl_examine_user_ids    抄送的人
     * sl_drill_date     训练日期
     * sl_drill_time     训练时间
     * sl_drill_lon      经度
     * sl_drill_lat      纬度
     * sl_drill_addr     训练地点
     * sl_other_coach    其他教练
     * sl_temperature    气温
     * sl_hum            湿度
     * sl_chaoxi         潮汐
     * sl_yongl          涌浪
     * sl_sleep          睡眠
     * sl_food           食欲
     * sl_jingsh         精神状态
     * sl_pilao          疲劳程度
     * sl_xunlian        训练欲望
     * sl_renzhen        认真程度
     * sl_pulse          晨脉
     * sl_after_pulse    训练后的脉搏
     * sl_ing_pulse      训练时的脉搏
     * sl_weight         体重
     * sl_wound          生化指标及伤病情况
     * sl_distance       航行距离
     * sl_max_speed      最高速度
     * sl_youxiao_time   有效训练时间
     * sl_youxiao_distance    有效训练距离
     * sl_max_fre         最高桨频
     * sl_max_pulse      最高心率
     * sl_summary_word   文字总结
     * sl_summary_images  图片总结
     * sl_summary_voice   音频总结
     *
     * sl_id              日志ID     第一次填写的时候传0
     */
    public function writeLog(){
        $params = input('');
        $data = [
            'sl_type' => $params['sl_type'],
            'sl_time' => $this->create_time
        ];
        
        // 如果抄送
//        if($params['sl_type'] == 2){
//            $data['sl_send_time'] = $this->create_time;
//            $data['sl_examine_user_ids'] = ',' . $params['sl_examine_user_ids'] . ',';
//        }
        // 训练日期
        if(isset($params['sl_drill_date'])){
            $data['sl_drill_date'] = $params['sl_drill_date'];
        }
        // 训练时间
        if(isset($params['sl_drill_time'])){
            $data['sl_drill_time'] = $params['sl_drill_time'];
        }
        // 经度
        if(isset($params['sl_drill_lon'])){
            $data['sl_drill_lon'] = $params['sl_drill_lon'];
        }
        // 纬度
        if(isset($params['sl_drill_lat'])){
            $data['sl_drill_lat'] = $params['sl_drill_lat'];
        }
        // 训练地点
        if(isset($params['sl_drill_addr'])){
            $data['sl_drill_addr'] = $params['sl_drill_addr'];
        }
        // 其他教练
        if(isset($params['sl_other_coach'])){
            $data['sl_other_coach'] = $params['sl_other_coach'];
        }
        // 气温
        if(isset($params['sl_temperature'])){
            $data['sl_temperature'] = $params['sl_temperature'];
        }
        // 湿度
        if(isset($params['sl_hum'])){
            $data['sl_hum'] = $params['sl_hum'];
        }
        // 潮汐
        if(isset($params['sl_chaoxi'])){
            $data['sl_chaoxi'] = $params['sl_chaoxi'];
        }
        // 涌浪
        if(isset($params['sl_yongl'])){
            $data['sl_yongl'] = $params['sl_yongl'];
        }
        // 睡眠
        if(isset($params['sl_sleep'])){
            $data['sl_sleep'] = $params['sl_sleep'];
        }
        // 食欲
        if(isset($params['sl_food'])){
            $data['sl_food'] = $params['sl_food'];
        }
        // 精神状态
        if(isset($params['sl_jingsh'])){
            $data['sl_jingsh'] = $params['sl_jingsh'];
        }
        // 疲劳程度
        if(isset($params['sl_pilao'])){
            $data['sl_pilao'] = $params['sl_pilao'];
        }
        // 训练欲望
        if(isset($params['sl_xunlian'])){
            $data['sl_xunlian'] = $params['sl_xunlian'];
        }
        // 认真程度
        if(isset($params['sl_renzhen'])){
            $data['sl_renzhen'] = $params['sl_renzhen'];
        }
        // 晨脉
        if(isset($params['sl_pulse'])){
            $data['sl_pulse'] = $params['sl_pulse'];
        }
        // 训练后的脉搏
        if(isset($params['sl_after_pulse'])){
            $data['sl_after_pulse'] = $params['sl_after_pulse'];
        }
        // 训练时脉搏
        if(isset($params['sl_ing_pulse'])){
            $data['sl_ing_pulse'] = $params['sl_ing_pulse'];
        }
        // 体重
        if(isset($params['sl_weight'])){
            $data['sl_weight'] = $params['sl_weight'];
        }
        // 生化指标及伤病情况
        if(isset($params['sl_wound'])){
            $data['sl_wound'] = $params['sl_wound'];
        }
        // 航行距离
        if(isset($params['sl_distance'])){
            $data['sl_distance'] = $params['sl_distance'];
        }
        // 最高速度
        if(isset($params['sl_max_speed'])){
            $data['sl_max_speed'] = $params['sl_max_speed'];
        }
        // 有效训练时间
        if(isset($params['sl_youxiao_time'])){
            $data['sl_youxiao_time'] = $params['sl_youxiao_time'];
        }
        // 有效训练距离
        if(isset($params['sl_youxiao_distance'])){
            $data['sl_youxiao_distance'] = $params['sl_youxiao_distance'];
        }
        // 最高桨频
        if(isset($params['sl_max_fre'])){
            $data['sl_max_fre'] = $params['sl_max_fre'];
        }
        // 最高心率
        if(isset($params['sl_max_pulse'])){
            $data['sl_max_pulse'] = $params['sl_max_pulse'];
        }
        // 文字总结
        if(isset($params['sl_summary_word'])){
            $data['sl_summary_word'] = $params['sl_summary_word'];
        }
        // 图片总结
        if(isset($params['sl_summary_images'])){
            $data['sl_summary_images'] = $params['sl_summary_images'];
        }
        // 音频总结
        if(isset($params['sl_summary_voice'])){
            $data['sl_summary_voice'] = $params['sl_summary_voice'];
        }
        if(isset($params['sl_voice_time'])){
            $data['sl_voice_time'] = $params['sl_voice_time'];
        }
        if(isset($params['sl_id']) && $params['sl_id'] == 0){
            $data['sl_drill_id'] = $params['drill_id'];
            $data['sl_user_id'] = $this->user_id;
            Db::startTrans();
            try{
                // 日志
                $id = Db::table('up_sailboat_drill_log')->insertGetId($data);
                // 训练表，写过日志
                $drill_info = Db::table('up_sailboat_drill')
                    ->where('drill_id', $params['drill_id'])
                    ->field('drill_write_log_user')
                    ->find();
                Db::table('up_sailboat_drill')
                    ->where('drill_id', $params['drill_id'])
                    ->setField('drill_write_log_user', $drill_info['drill_write_log_user'] . $this->user_id . ',');
                Db::commit();
                return json(['status' => 200, 'msg' => $id]);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '保存失败']);
            }
        }else{
            $res = Db::table('up_sailboat_drill_log')->where('sl_id', $params['sl_id'])->update($data);
            if($res){
                return json(['status' => 200, 'msg' => '保存成功']);
            }else{
                return json(['status' => 0, 'msg' => '保存失败']);
            }
        }
    }
    
    /**
     * 发送审批
     * @return \think\response\Json
     * sl_id     日志ID
     * sl_examine_user_ids   发送审批的人ID
     */
    public function addExamine(){
        $sl_id = input('sl_id');
        $sl_examine_user_ids = input('sl_examine_user_ids');
        $log = Db::table('up_sailboat_drill_log')
            ->where('sl_id', $sl_id)
            ->field('sl_examine_user_ids')
            ->find();
        $update['sl_send_time'] = $this->create_time;
        $update['sl_type'] = 2;
        if(!$log['sl_examine_user_ids']){
            $update['sl_examine_user_ids'] = ',' . $sl_examine_user_ids . ',';
        }else{
            $old = explode(',', trim($log['sl_examine_user_ids'], ','));
            $new = explode(',', $sl_examine_user_ids);
            $user_arr = array_unique(array_merge($old, $new));
            $update['sl_examine_user_ids'] = ',' . implode(',', $user_arr) . ',';
        }
        $res = Db::table('up_sailboat_drill_log')->where('sl_id', $sl_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '发送成功']);
        }else{
            return json(['status' => 0, 'msg' => '发送失败']);
        }
    }
    
    /**
     * 日志信息回显
     * @return \think\response\Json
     * drill_id      训练ID，如果是从训练列表进入的，传drill_id
     * sl_id         日志ID,如果是从日志列表进入的，传sl_id
     */
    public function showLog(){
        $drill_id = input('drill_id');
        $sl_id = input('sl_id');
        // 如果是从训练列表进入的，需要传训练ID
        if(isset($drill_id) && $drill_id != ''){
            $where['sl_drill_id'] = $drill_id;
            $where['sl_user_id'] = $this->user_id;
        }else{ // 如果是从日志列表进入的，则需要传日志的ID
            $where['sl_id'] = $sl_id;
        }
        $info = Db::table('up_sailboat_drill_log')
            ->join('up_sailboat_drill', 'drill_id = sl_drill_id', 'left')
            ->join('up_u_user', 'user_id = drill_create_user_id', 'left')
            ->where($where)
            ->field('sl_id, sl_drill_date, sl_drill_time, sl_drill_lon, sl_drill_lat, sl_drill_addr, 
            user_username, sl_other_coach, sl_temperature, drill_wind, drill_flow_direction, drill_flow_speed, 
            sl_chaoxi, sl_yongl, sl_sleep, sl_food, sl_jingsh, sl_pilao, sl_xunlian, sl_renzhen, 
            sl_pulse, sl_weight, sl_ing_pulse, sl_after_pulse, sl_wound, sl_distance, sl_youxiao_time, 
            sl_max_speed, sl_youxiao_distance, sl_max_fre, sl_max_pulse, sl_summary_word, sl_summary_images, 
            sl_summary_voice, drill_plan, drill_zongjie, sl_hum, sl_voice_time')
            ->find();
        if($info){
            return json(['status' => 200, 'msg' => $info]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 日志评论
     * @return \think\response\Json
     * sl_id      日志ID
     * range      1 看两条   2 更多
     */
    public function logComment(){
        $sl_id = input('sl_id');
        $range = input('range'); // 1 看两条   2 更多
        $log = Db::table('up_sailboat_drill_log')
            ->where('sl_id', $sl_id)
            ->field('sl_user_id')
            ->find();
        $comment = Db::table('up_sailboat_drill_log_comment')
            ->join('up_u_user', 'user_id = lc_user_id', 'left')
            ->field('user_username, user_image, lc_content, lc_image')
            ->order('lc_time desc')
            ->where('lc_log_id', $sl_id);
        // 如果不是我发布的日志，只能看到我评论的内容
        if($log['sl_user_id'] != $this->user_id){
            $comment->where('lc_user_id', $this->user_id);
        }
        if($range == 1){
            $comment->limit(2);
        }else{
            $comment->page($this->page, 10);
        }
        $comment = $comment->select();
        if($comment){
            foreach($comment as $k => $v){
                $imgs = trim($v['lc_image'], ',');
                $comment[$k]['lc_image'] = explode(',', $imgs);
            }
            return json(['status' => 200, 'msg' => $comment]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 评论日志
     * @return \think\response\Json
     * log_id       被评论的日志ID
     * lc_content   评论的内容
     * lc_image     评论的图片
     */
    public function writeComment(){
        $log_id = input('log_id');
        $lc_content = input('lc_content');
        $lc_image = input('lc_image');
        $insert = [
            'lc_user_id' => $this->user_id,
            'lc_log_id' => $log_id,
            'lc_content' => $lc_content,
            'lc_image' => $lc_image,
            'lc_time' => $this->create_time
        ];
        $res = Db::table('up_sailboat_drill_log_comment')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '评论成功']);
        }else{
            return json(['status' => 0, 'msg' => '评论失败']);
        }
    }
    
    /**
     * 我创建的训练
     * @return \think\response\Json
     * keyword      搜索关键字
     * drill_start_time    查询开始时间
     * drill_end_time      查询结束时间
     * page                页码
     */
    public function myCreateDrill(){
        $keyword = trim(input('keyword'), ' ');
        $drill_start_time = input('drill_start_time');
        $drill_end_time = input('drill_end_time');
        $where['drill_create_user_id'] = $this->user_id;
//        $where['drill_plan'] = ['NEQ', ''];
//        $where['drill_zongjie'] = ['NEQ', ''];
        if(isset($keyword) && $keyword != ''){
            $where['drill_city|user_username'] = ['like', '%' . $keyword . '%'];
        }
        if(isset($drill_start_time) && $drill_start_time != '' && isset($drill_end_time) && $drill_end_time != ''){
            $where['drill_create_date'] = ['between', [$drill_start_time, $drill_end_time]];
        }
        $list = Db::table('up_sailboat_drill')
            ->join('up_u_user', 'user_id = drill_create_user_id', 'left')
            ->where($where)
            ->where(function ($query){
                $query->where(function($query1){
                    $query1->where(['drill_plan' => ['NEQ', ''], 'drill_zongjie' => ['NEQ', '']]);
                })->whereOr(function ($query2) {
                    $query2->where('drill_is_match', 2);
                });
            })
            ->page($this->page, 10)
            ->field('drill_id, drill_city, drill_create_date, drill_other_coach, user_username')
            ->order('drill_date desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我参加的训练
     * @return \think\response\Json
     * keyword      搜索关键字
     * drill_start_time    查询开始时间
     * drill_end_time      查询结束时间
     * page                页码
     */
    public function myJoinDrill(){
        $keyword = trim(input('keyword'), ' ');
        $drill_start_time = input('drill_start_time');
        $drill_end_time = input('drill_end_time');
        $where['drill_join_user_id'] = ['like', '%,' . $this->user_id . ',%'];
//        $where['drill_write_log_user'] = ['like', '%,' . $this->user_id . ',%'];
        if(isset($keyword) && $keyword != ''){
            $where['drill_city|user_username'] = ['like', '%' . $keyword . '%'];
        }
        if(isset($drill_start_time) && $drill_start_time != '' && isset($drill_end_time) && $drill_end_time != ''){
            $where['drill_create_date'] = ['between', [$drill_start_time, $drill_end_time]];
        }
        $list = Db::table('up_sailboat_drill')
            ->join('up_u_user', 'user_id = drill_create_user_id', 'left')
            ->where($where)
            ->where(function($query){
                $query->where('drill_write_log_user', 'like', "%,$this->user_id,%")
                    ->whereOr('drill_is_match', 2);
            })
            ->page($this->page, 10)
            ->field('drill_id, drill_city, drill_create_date, drill_other_coach, user_username')
            ->order('drill_date desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我批阅的训练
     * @return \think\response\Json
     * keyword      搜索关键字
     * drill_start_time    查询开始时间
     * drill_end_time      查询结束时间
     * page                页码
     */
    public function myExamineDrill(){
        $keyword = trim(input('keyword'), ' ');
        $drill_start_time = input('drill_start_time');
        $drill_end_time = input('drill_end_time');
        $drill_ids = Db::table('up_sailboat_drill_log')
            ->join('up_u_user', 'sl_user_id = user_id', 'left')
            ->where('sl_examine_user_ids', 'like', '%,' . $this->user_id . ',%')
            ->group('sl_drill_id')
            ->column('sl_drill_id');
        $where['drill_id'] = ['IN', $drill_ids];
        
        if(isset($keyword) && $keyword != ''){
            $where['drill_city|user_username'] = ['like', '%' . $keyword . '%'];
        }
        if(isset($drill_start_time) && $drill_start_time != '' && isset($drill_end_time) && $drill_end_time != ''){
            $where['drill_create_date'] = ['between', [$drill_start_time, $drill_end_time]];
        }
        $list = Db::table('up_sailboat_drill')
            ->join('up_u_user', 'user_id = drill_create_user_id', 'left')
            ->where($where)
            ->page($this->page, 10)
            ->field('drill_id, drill_city, drill_create_date, drill_other_coach, user_username')
            ->order('drill_date desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我批阅的日志
     * @return \think\response\Json
     */
    public function myExamineLog(){
        $drill_id = input('drill_id');
        if(isset($drill_id) && $drill_id != ''){
            $where['sl_drill_id'] = $drill_id;
        }else{
            $where['sl_examine_user_ids'] = ['like', '%,' . $this->user_id . ',%'];
        }
        $list = Db::table('up_sailboat_drill_log')
            ->join('up_u_user', 'sl_user_id = user_id', 'left')
//            ->where('sl_examine_user_ids', 'like', '%,' . $this->user_id . ',%')
            ->where($where)
            ->field('sl_id, sl_send_time, user_username, user_image, sl_time')
            ->page($this->page, 20)
            ->order('sl_send_time desc')
            ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['sl_send_time'] = date('Y-m-d H:i', strtotime($v['sl_send_time']));
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 训练发起人
     * @return \think\response\Json
     * drill_id     训练ID
     */
    public function drillCreateUser(){
        $drill_id = input('drill_id');
        $create_user_id = Db::table('up_sailboat_drill')
            ->where('drill_id', $drill_id)
            ->value('drill_create_user_id');
        return json(['status' => 200, 'msg' => $create_user_id]);
    }
    
    /**
     * 训练距离
     * @return \think\response\Json
     * dd_cate     类型   1 帆船   2 赛艇
     * dd_distance    距离
     */
    public function drillDistance(){
        $dd_cate = input('dd_cate'); // 1 帆船  2 赛艇
        $dd_distance = input('dd_distance');
        $insert = [
            'dd_user_id' => $this->user_id,
            'dd_cate' => $dd_cate,
            'dd_distance' => $dd_distance
        ];
        $res = Db::table('up_sailboat_drill_distance')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => false]);
        }
    }
    
    /**
     * 转发到风讯的链接
     * @param $sl_id     日志ID
     * @return \think\response\Json
     */
    public function getUrl($sl_id){
        $url = http_type() . '/api/Drill/showLog?sl_id=' . $sl_id;
        return json(['status' => 200, 'msg' => $url]);
    }
    
    /**
     * 转发到风讯
     * @return \think\response\Json
     * info_show     可见人群   1 公开  2 粉丝  3 好友  4 本人可见
     * info_friend_user    可见的好友
     * info_link    内容
     * info_content   发布内容
     * info_addr      发布的地址
     * info_lon     经度
     * info_lat     纬度
     */
    public function publishInformation(){
        $params = input('');
        $insert = [
            'info_user_id' => $this->user_id,
//            'info_show' => $params['info_show'],
            'info_is_publish' => 2,
            'info_time' => $this->create_time,
            'info_type' => 1,
            'info_link' => $params['info_link']
        ];
        if(isset($params['info_content']) && !empty($params['info_content'])){
            $insert['info_content'] = $params['info_content'];
        }
        if(isset($params['info_show']) && !empty($params['info_show'])){
            $insert['info_show'] = $params['info_show'];
        }
        if(isset($params['info_addr']) && !empty($params['info_addr'])){
            $insert['info_addr'] = $params['info_addr'];
            $insert['info_lon'] = $params['info_lon'];
            $insert['info_lat'] = $params['info_lat'];
        }
        if($params['info_show'] == 3){
            $insert['info_friend_user'] = ',' . $params['info_friend_user'] . ',';
        }
        $res = Db::table('up_information')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 更新教练当前说话的组
     * @return \think\response\Json
     * zu_id       分组ID，总队为0
     * drill_id   训练ID
     */
    public function createUserZu(){
        $zu_id = input('zu_id'); // 分组ID
        $drill_id = input('drill_id'); // 训练ID
        $res = Db::table('up_sailboat_drill')->where('drill_id', $drill_id)->setField('drill_call_id', $zu_id);
        if($res){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => false]);
        }
    }
    
    /**
     * 天气（聚合数据）
     * @param $cityname   城市名称
     * @return \think\response\Json
     */
    public function temperature($cityname){
        header('Content-type:text/html;charset=utf-8');
        //配置您申请的appkey
        $appkey = "6055a8b7653920bbc8f4936f292836f9";

        //************1.根据城市查询天气************
        $url = "http://op.juhe.cn/onebox/weather/query";
        $params = array(
            "cityname" => $cityname,//要查询的城市，如：南京
            "key" => $appkey,//应用APPKEY(应用详细页查询)
            "dtype" => "json",//返回数据的格式,xml或json，默认json
        );
        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);
        if($result){
            if($result['error_code']=='0'){
                return json(['status' => 200, 'msg' => $result['result']]);
            }else{
                $msg = $result['error_code'].":".$result['reason'];
                return json(['status' => 0, 'msg' => $msg]);
            }
        }else{
            return json(['status' => 0, 'msg' => '请求失败']);
        }
    }
}